//
//  XOWMViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/25.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMViewController.h"

@interface XOWMViewController ()<YJDNaviBarDelegate>

@end

@implementation XOWMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (XOWMNaviBar *)naviBar
{
    if (!_naviBar)
    {
        _naviBar = [[XOWMNaviBar alloc] init];
        _naviBar.delegate = self;
        [self.view addSubview:_naviBar];
        _naviBar.c_left.equal(self.view);
        _naviBar.c_right.equal(self.view);
        _naviBar.c_height.equal(BUTTONHEIGHT);
        if ([UIApplication sharedApplication].statusBarHidden)
        {
            _naviBar.c_top.equal(self.view);
        }
        else
        {
            [self setStatusBarBackgroundColor:[UIColor whiteColor]];
            _naviBar.c_top.equal(self.view).with.coffset(STATUSHEIGHT);
        }
    }
    return _naviBar;
}

- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (UIViewController *)yjd_currentViewController
{
    UIWindow *window =  [UIView yjd_keyWindow];
    UIViewController *vc = window.rootViewController;
    while (vc.presentedViewController)
    {
        vc = vc.presentedViewController;
        if ([vc isKindOfClass:[UINavigationController class]])
        {
            vc = [(UINavigationController *)vc visibleViewController];
        }
        else if ([vc isKindOfClass:[UITabBarController class]])
        {
            vc = [(UITabBarController *)vc selectedViewController];
        }
    }
    return vc;
}

- (void)yjd_pop
{
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    UIViewController *vc = [self yjd_currentViewController];
    if (vc)
    {
        if (vc!= self)
        {
            NSLog(@"当前窗口===》。 %@",self);
            return [vc presentViewController:self animated:YES completion:nil];
        }
        NSLog(@"准备推出的控制器和已经推出的是同一个===》。 %@",self);
    }
}

- (void)yjd_keyboardWillShow:(NSNotification *)notifi
{
    //获取键盘的高度
    NSDictionary *userInfo = [notifi userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardHeight = [aValue CGRectValue].size.height;//键盘的高度
    //获取键盘动画时间
    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //输入框在当前屏幕的坐标y
    CGFloat maxY = 0;
    UIView *view = [self yjd_findFirstResponder];
    if (view == nil || [view isKindOfClass:NSClassFromString(@"WKContentView")])
        return;
    maxY =  CGRectGetMaxY([view convertRect:view.bounds toView:[[[UIApplication sharedApplication] delegate] window]]);
    
    //判断是非遮挡当前输入框，小于0遮挡，大于或等于0没有
    CGFloat map = SCREEN_HEIGHT - maxY - keyboardHeight;
    if (map < 0)
    {
        [UIView animateWithDuration:time animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, map);
        }];
    }
}

- (void)yjd_keyboardWillHide:(NSNotification *)notifi
{
    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:time animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}

- (id)yjd_findFirstResponder
{
    UIWindow *window =  [UIView yjd_keyWindow];
    id responder = [window performSelector:@selector(firstResponder)];
    if ([responder isKindOfClass:[UIView class]])
        return responder;
    else if ([responder isKindOfClass:[UIViewController class]])
        return [responder view];
    else if ([responder isKindOfClass:NSClassFromString(@"WKContentView")])
        return nil;
    else
        return nil;
}

- (UIView *)yjd_keyBoard
{
    return [self.view yjd_findKeyBoard];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

#pragma mark - YJDNaviBarDelegate
- (void)yjd_methodForNaviBarDelegate
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yjd_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yjd_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [UIView animateWithDuration:0.5 animations:^{
        self.view.alpha = 1.0;
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [CCHud stopHud];
    [self.view endEditing:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [UIView animateWithDuration:0.2 animations:^{
        self.view.alpha = 0.0;
    }];
}
- (void)dealloc
{
    [CCHud stopHud];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_getLoginState:(NSString *)mediaInfo {
    NSLog(@"Get Info Failed");
}
@end
